package com.qiaoba.module.system.controller;

import com.github.pagehelper.PageHelper;
import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.module.system.entity.SysPost;
import com.qiaoba.module.system.entity.dto.SysPostDTO;
import com.qiaoba.module.system.entity.param.SysPostParam;
import com.qiaoba.module.system.service.SysPostService;
import com.qiaoba.poi.excel.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 岗位管理 web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-09-22 04:20:28
 */
@Validated
@RestController
@RequestMapping("/system/post")
@RequiredArgsConstructor
@Tag(name = "岗位管理")
public class SysPostController {

    private final SysPostService sysPostService;

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:post:list')")
    @Operation(summary = "获取列表")
    public PageResult<SysPost> list(@Validated @ParameterObject SysPostParam param, @ParameterObject PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysPost> list = sysPostService.selectList(param);
        return PageResult.build(list);
    }

    @GetMapping(value = "/{postId}")
    @PreAuthorize("hasAuthority('system:post:query')")
    @Operation(summary = "获取详情")
    public Result<SysPost> getInfo(@Size(max = 20, message = "ID不合法") @PathVariable("postId") String postId) {
        return Result.success(sysPostService.selectById(postId, false));
    }

    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    @PreAuthorize("hasAuthority('system:post:add')")
    @Operation(summary = "新增岗位")
    public Result add(@Validated(AddGroup.class) @RequestBody SysPostDTO dto) {
        return Result.toAjax(sysPostService.insert(dto));
    }

    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @PreAuthorize("hasAuthority('system:post:edit')")
    @Operation(summary = "修改岗位")
    public Result edit(@Validated(EditGroup.class) @RequestBody SysPostDTO dto) {
        return Result.toAjax(sysPostService.updateById(dto));
    }

    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @PreAuthorize("hasAuthority('system:post:remove')")
    @Operation(summary = "删除岗位")
    public Result remove(@PathVariable List<String> ids) {
        return Result.toAjax(sysPostService.deleteByIds(ids));
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @PreAuthorize("hasAuthority('system:post:export')")
    @Operation(summary = "导出岗位")
    public void export(HttpServletResponse response, @Validated SysPostParam param) {
        List<SysPost> list = sysPostService.selectList(param);
        ExcelUtil.exportExcel(list, SysPost.class, "岗位数据", response);
    }
}
